package cn.tinlan.shopcoin;

import android.content.Intent;
import android.support.annotation.Nullable;
import android.support.v4.app.Fragment;
import android.support.v4.app.FragmentTransaction;
import android.support.v4.view.ViewPager;
import android.support.v7.app.AppCompatActivity;
import android.os.Bundle;
import android.util.Log;

import com.winton.bottomnavigationview.NavigationItemView;
import com.winton.bottomnavigationview.NavigationView;

import java.util.ArrayList;
import java.util.List;

import butterknife.BindView;
import butterknife.ButterKnife;
import cn.tinlan.shopcoin.Utils.SPUtils;
import cn.tinlan.shopcoin.app.car.CarFragment;
import cn.tinlan.shopcoin.app.chat.ChatFragment;
import cn.tinlan.shopcoin.app.discover.DiscoverFragment;
import cn.tinlan.shopcoin.app.home.HomeFragment;
import cn.tinlan.shopcoin.app.koi.KoiFragment;
import cn.tinlan.shopcoin.app.login.LoginActivity;
import cn.tinlan.shopcoin.app.mine.MineFragment;
import cn.tinlan.shopcoin.base.BaseActivity;
import cn.tinlan.shopcoin.base.BaseFragment;
import cn.tinlan.shopcoin.widget.bottomnv.NoScrollViewPager;
import cn.tinlan.shopcoin.widget.bottomnv.TabPagerAdapter;

public class MainActivity extends BaseActivity{



//    @BindView(R.id.view_pager)
//    public NoScrollViewPager mViewPager;
//    @BindView(R.id.home_container)

    @BindView(R.id.nv)
    public NavigationView mBottomTab;


    private List<NavigationView.Model> tabs ;
    private List<BaseFragment> mFragmentList;
//    private TabPagerAdapter mNVAdapter;
    BaseFragment currentFragment;
    FragmentTransaction transaction;
    public void initView() {

        configViewPage();
        configBottomNavigationView();


    }



    @Override
    public void initData() {
        super.initData();
    }

    @Override
    protected int provideContentViewId() {
        return R.layout.activity_main;
    }

    /*配置显示内容*/
    public void configViewPage() {
        mFragmentList = new ArrayList<>();
        mFragmentList.add(new HomeFragment());
//        mFragmentList.add(new DiscoverFragment());
        mFragmentList.add(new KoiFragment());
//        mFragmentList.add(new CarFragment());
        mFragmentList.add(new ChatFragment());
        mFragmentList.add(new MineFragment());
//        mNVAdapter = new TabPagerAdapter(getSupportFragmentManager(),mFragmentList);
//        mViewPager.setAdapter(mNVAdapter);
    }

    /*配置底部导航栏*/
    public void configBottomNavigationView() {
        tabs = new ArrayList<>();
        tabs.add(new NavigationView.Model.Builder(R.drawable.home_select,R.drawable.home_normal).title("首页").build());
        tabs.add(new NavigationView.Model.Builder(R.drawable.dcc_select,R.drawable.dcc_normal).title("锦鲤").build());
        tabs.add(new NavigationView.Model.Builder(R.drawable.notice_select,R.drawable.notice_normal).title("信息").build());
//        tabs.add(new NavigationView.Model.Builder(R.drawable.dcc,R.drawable.dcc).title("锦鲤").build());
//        tabs.add(new NavigationView.Model.Builder(R.drawable.tab_chat_select,R.drawable.tab_chat_normal).title("购物车").build());
        tabs.add(new NavigationView.Model.Builder(R.drawable.mine_select,R.drawable.mine_normal).title("我").build());
        mBottomTab.setItems(tabs);



        mBottomTab.setOnTabSelectedListener(new NavigationView.OnTabSelectedListener() {
            @Override
            public void selected(int i, NavigationView.Model model) {
//                mViewPager.setCurrentItem(i);
                switchFragment(i,model);
            }

            @Override
            public void unselected(int i, NavigationView.Model model) {

            }
        });
        mBottomTab.build();
//        mBottomTab.reminder(0,true,"shouye");
    }

    private void switchFragment(int i, NavigationView.Model model) {
        if (i !=0) {
            if (!SPUtils.getIsLogin(MainActivity.this)){
//                mBottomTab.check(0);
//                mBottomTab.reminder();
                Intent intent = new Intent(MainActivity.this,LoginActivity.class);
                intent.putExtra("index",i);
                startActivityForResult(intent,1861);
//                startActivity(new Intent(MainActivity.this, LoginActivity.class));
                return;
            }
        }

        BaseFragment fragment = mFragmentList.get(i);

        if (currentFragment == fragment) return;

        transaction = getSupportFragmentManager().beginTransaction();
        if(currentFragment == null) {

            transaction.replace(R.id.home_container,fragment).commit();

        }else {
            if (!fragment.isAdded()){
                transaction.add(R.id.home_container,mFragmentList.get(i)).hide(currentFragment).commit();
            }else {
                transaction.show(mFragmentList.get(i)).hide(currentFragment).commit();
            }
        }

        currentFragment = mFragmentList.get(i);;
    };

    @Override
    protected void onActivityResult(int requestCode, int resultCode, @Nullable Intent data) {
        super.onActivityResult(requestCode, resultCode, data);

        Log.d("resultcode",resultCode+"");
        if (resultCode == 1851) {
            int currentIndex = data.getIntExtra("resultIndex",0);
            switchFragment(currentIndex,null);
        }

    }


    //    @Override
//    protected void onStart() {
//        super.onStart();
//        int currentFrament = getIntent().getIntExtra("currentFragment",0);
//
////        String test = getIntent().getStringExtra("test");
////        Log.d("mainactivity0000000000",currentFrament+test);
//        if (currentFrament != 0) {
//            mBottomTab.check(currentFrament);
////            switchFragment(currentFrament,null);
//        }
//    }
}
